package com.zrc.superframe_base.mvx.base

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.view.animation.Animation
import androidx.fragment.app.Fragment
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.components.SimpleImmersionOwner
import com.gyf.immersionbar.components.SimpleImmersionProxy
import com.zrc.superframe_aop.annotation.SingleClick
import me.yokeyword.fragmentation.*
import me.yokeyword.fragmentation.anim.FragmentAnimator

/**
 * @Author: superman
 * @CreateTime: 2020/7/4
 * @Describe: BaseFragment基类
 */
abstract class BaseFragment : Fragment() , ISupportFragment, SimpleImmersionOwner ,View.OnClickListener{

    private var mSimpleImmersionProxy= SimpleImmersionProxy(this)//ImmersionBar代理类

    val mDelegate = SupportFragmentDelegate(this)

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mDelegate.onAttach(activity)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        mDelegate.onCreate(savedInstanceState)
    }

    open fun isRegisterEvent(): Boolean = false

    @SuppressLint("CheckResult")
    fun requestPermission(vararg permissions: String, success: (() -> Unit)? = null, failed: (() -> Unit)? = null) {
        activity?.let {
            (it as BaseActivity).requestPermission(*permissions, success = success, failed = failed)
        }
    }

    override fun getSupportDelegate(): SupportFragmentDelegate? =mDelegate

    override fun extraTransaction(): ExtraTransaction? =mDelegate.extraTransaction()

    override fun onCreateAnimation(transit: Int, enter: Boolean, nextAnim: Int): Animation?=mDelegate.onCreateAnimation(transit, enter, nextAnim)

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mSimpleImmersionProxy.onActivityCreated(savedInstanceState)
        mDelegate.onActivityCreated(savedInstanceState)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mDelegate.onSaveInstanceState(outState)
    }

    override fun onResume() {
        super.onResume()
        mDelegate.onResume()
    }

    override fun onPause() {
        super.onPause()
        mDelegate.onPause()
    }

    override fun onDestroyView() {
        mDelegate.onDestroyView()
        super.onDestroyView()
        mSimpleImmersionProxy.onDestroy()
    }

    override fun onDestroy() {
        mDelegate.onDestroy()
        super.onDestroy()
    }

    var isShow=false
    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        isShow=!hidden
        mDelegate.onHiddenChanged(hidden)
        mSimpleImmersionProxy.onHiddenChanged(hidden)
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        isShow=isVisibleToUser
        mDelegate.setUserVisibleHint(isVisibleToUser)
        mSimpleImmersionProxy.isUserVisibleHint = isVisibleToUser
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        mSimpleImmersionProxy.onConfigurationChanged(newConfig)
    }

    override fun enqueueAction(runnable: Runnable?) {
        mDelegate.enqueueAction(runnable)
    }

    override fun post(runnable: Runnable?) {
        mDelegate.post(runnable)
    }

    override fun onEnterAnimationEnd(savedInstanceState: Bundle?) {
        mDelegate.onEnterAnimationEnd(savedInstanceState)
    }

    override fun onLazyInitView(savedInstanceState: Bundle?) {
        mDelegate.onLazyInitView(savedInstanceState)
    }

    override fun onSupportVisible() {
        mDelegate.onSupportVisible()
    }

    override fun onSupportInvisible() {
        mDelegate.onSupportInvisible()
    }

    override fun isSupportVisible(): Boolean =mDelegate.isSupportVisible

    override fun onCreateFragmentAnimator(): FragmentAnimator? =mDelegate.onCreateFragmentAnimator()

    override fun getFragmentAnimator(): FragmentAnimator? =mDelegate.fragmentAnimator

    override fun setFragmentAnimator(fragmentAnimator: FragmentAnimator?) {
        mDelegate.fragmentAnimator = fragmentAnimator
    }

    override fun onBackPressedSupport(): Boolean =mDelegate.onBackPressedSupport()

    override fun setFragmentResult(resultCode: Int, bundle: Bundle?) {
        mDelegate.setFragmentResult(resultCode, bundle)
    }

    override fun onFragmentResult(requestCode: Int, resultCode: Int, data: Bundle?) {
        mDelegate.onFragmentResult(requestCode, resultCode, data)
    }

    override fun onNewBundle(args: Bundle?) {
        mDelegate.onNewBundle(args)
    }

    override fun putNewBundle(newBundle: Bundle?) {
        mDelegate.putNewBundle(newBundle)
    }

    override fun immersionBarEnabled() = false

    override fun initImmersionBar() {
        //初始化沉浸式
    }

    @SingleClick(value = 1000)
    override fun onClick(view: View) {

    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        childFragmentManager.fragments.forEach {
            it.onActivityResult(requestCode, resultCode, data)
        }
    }

}